<template>
  <div style="width: 90%;margin-top: 20px; margin-left: 20px">
    <el-button type="primary" @click="showAdd"
      >创建实验步骤</el-button
    >
    <el-table
      :data="exprStepLIst"
      style=" margin-top: 20px"
      :stripe="true"
      border
    >
      <el-table-column prop="expeId" label="实验ID"> </el-table-column>
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column prop="content" label="内容"> </el-table-column>
      <el-table-column prop="inputCode" label="输入代码"> </el-table-column>
      <el-table-column prop="output" label="运行结果"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click="edit(scope.row)" type="primary" size="small"
            >编辑</el-button
          >
          <el-button type="danger" size="small" @click="deleteObj(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页栏 -->
    <el-pagination
      layout="total, sizes, prev, pager, next, jumper"
      :page-size="pageSize"
      :page-sizes="[5, 10]"
      :total="total"
      :current-page="currentPage"
      @current-change="changeCurrent"
      @size-change="changeSize"
      align="center"
      style="margin-top: 10px"
    ></el-pagination>
    <!-- 编辑框 -->
    <el-dialog title="修改实验步骤" :visible.sync="editVisible">
      <el-form label-width="80px">
        <el-form-item label="标题">
          <el-input v-model="expeStepObject.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input type="textarea" v-model="expeStepObject.content"></el-input>
        </el-form-item>
        <el-form-item label="输入代码">
          <el-input
            type="textarea"
            v-model="expeStepObject.inputCode"
          ></el-input>
        </el-form-item>
        <el-form-item label="输出结果">
          <el-input type="textarea" v-model="expeStepObject.output"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">确认</el-button>
          <el-button @click="editVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 新增对话框 -->
    <el-dialog title="创建实验步骤" :visible.sync="addVisible">
      <el-form label-width="80px">
        <el-form-item label="实验名称">
          <el-select v-model="expeStepObject.expeId" placeholder="请选择">
            <el-option
              v-for="item in exprInfoList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="标题">
          <el-input v-model="expeStepObject.title"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input type="textarea" v-model="expeStepObject.content"></el-input>
        </el-form-item>
        <el-form-item label="输入代码">
          <el-input
            type="textarea"
            v-model="expeStepObject.inputCode"
          ></el-input>
        </el-form-item>
        <el-form-item label="输出结果">
          <el-input type="textarea" v-model="expeStepObject.output"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="toCreate">确认</el-button>
          <el-button @click="addVisible = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      exprStepLIst: [],
      expeStepObject: {},
      editVisible: false,
      addVisible: false,
      // 当前页码
      currentPage: 1,
      // 当前页面条数
      pageSize: 5,
      total: 0,
      exprInfoList: [],
    };
  },
  methods: {
    // 删除
    deleteObj(row) {
      this.$http({
        url: "/expe-step/delete/" + row.id,
        method: "post",
      })
        .then((response) => {
          if (response.data.code == 0) {
            this.$message({
              message: response.data.message,
              type: "success",
            });
            this.getexprStepLIst();
          } else {
            this.$message({
              message: response.data.message,
              type: "error",
            });
          }
        })
        .catch((err) => err);
    },
    // 编辑
    edit(row) {
      this.editVisible = true;
      this.$http({
        url: "expe-step/" + row.id,
        method: "get",
      })
        .then((response) => {
          if (response.data.code == 0) {
            this.expeStepObject = response.data.data;
          }
        })
        .catch((err) => err);
    },
    // 提交修改
    onSubmit() {
      this.$http({
        url: "/expe-step/edit",
        method: "post",
        data: this.expeStepObject,
      })
        .then((response) => {
          if (response.data.code == 0) {
            this.$message({
              message: response.data.message,
              type: "success",
            });
          } else {
            this.$message({
              message: response.data.message,
              type: "error",
            });
          }
          // 关闭
          this.editVisible = false;
          this.getexprStepLIst();
          this.expeStepObject = {};
        })
        .catch((err) => err);
    },
    showAdd() {
      this.addVisible = true;
      this.getAllExprInfo();
    },
    // 创建
    toCreate() {
      this.$http({
        url: "/expe-step/add",
        method: "post",
        data: this.expeStepObject,
      })
        .then((response) => {
          if (response.data.code == 0) {
            this.$message({
              message: response.data.message,
              type: "success",
            });
          } else {
            this.$message({
              message: response.data.message,
              type: "error",
            });
          }
          // 关闭
          this.addVisible = false;
          this.getexprStepLIst();
          this.expeStepObject = {};
        })
        .catch((err) => err);
    },
    // 发送请求，分页获取实验步骤信息
    getexprStepLIst() {
      this.$http({
        url:
          "expe-step/list?pageSize=" +
          this.pageSize +
          "&pageNum=" +
          this.currentPage,
        method: "get",
      })
        .then((response) => {
          if (response.data.code == 0) {
            this.exprStepLIst = response.data.data.data;
            this.total = response.data.data.total;
          }
        })
        .catch((err) => err);
    },
    // 获取所有的实验信息
    getAllExprInfo() {
      this.$http({
        url: "experiment/listAll",
        method: "get",
      })
        .then((response) => {
          if (response.data.code == 0) {
            this.exprInfoList = response.data.data;
          }
        })
        .catch((err) => err);
    },
    // 改变当前的页码
    changeCurrent(currentPage) {
      this.currentPage = currentPage;
      // 重新发送请求，获取数据集
      this.getexprStepLIst();
    },
    // 改变当前的显示条数
    changeSize(pageSize) {
      this.pageSize = pageSize;
      this.getexprStepLIst();
    },
  },
  mounted() {
    this.getexprStepLIst();
  },
};
</script>

<style>
</style>